﻿using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using WWLauncher.Constant;
using WWLauncher.Util;

namespace WWLauncher
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {

        System.Threading.Mutex mutex;
        public App()
        {
            this.Startup += App_Startup;
            this.DispatcherUnhandledException += Current_DispatcherUnhandledException;
        }

        private void App_Startup(object sender, StartupEventArgs e)
        {
            if (e.Args.Contains("-restart"))
            {
                StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
                return;
            }
            if (!e.Args.Any() || e.Args.Contains("-main"))
            {
                StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
                mutex = new System.Threading.Mutex(true, Constants.AppName, out bool ret);
                if (!ret)
                {
                    MessageUtil.SendMsgByWName(
                        Constants.AppName,
                        "DisplayApp"
                    );
                    Environment.Exit(0);
                }
            }
            else if (e.Args.Contains("-test"))
            {
                StartupUri = new Uri("test.xaml", UriKind.Relative); return;
            }
            else if (e.Args.Contains("-rebuild"))
            {
                AttachConsole(ATTACH_PARENT_PROCESS);
                new Startup.Startup().Start();
                Console.WriteLine("操作完成。");
            }
            else if (e.Args.Contains("-h") || e.Args.Contains("-?") || e.Args.Contains("-help"))
            {
                AttachConsole(ATTACH_PARENT_PROCESS);
                Console.WriteLine();
                Console.WriteLine("-main\t主窗体。");
                Console.WriteLine("-test\tTest窗体。");
                Console.WriteLine("-rebuild\t重建初始数据。");
                Console.WriteLine("-main\t主窗体。");
                Console.WriteLine("-h\t显示帮助信息。");
            }

        }

        void Current_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
            Mouse.OverrideCursor = null;
            LogUtil.WriteErrorLog(e.Exception, "未捕获异常!");
        }

        public static void DoEvents()
        {
            var frame = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
                new DispatcherOperationCallback(
                    delegate (object f)
                    {
                        ((DispatcherFrame)f).Continue = false;
                        return null;
                    }), frame);
            Dispatcher.PushFrame(frame);
        }
        private const int ATTACH_PARENT_PROCESS = -1;
        [DllImport("Kernel32.dll", EntryPoint = "AttachConsole", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern void AttachConsole(int dwProcessId);

    }

}
